package com.lagou.edu.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author stevejson
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Exchange exchangeOrderDie(){
        return new DirectExchange("ex.order.die", true, false);
    }
    @Bean
    public Queue queueOrderDie(){
        return new Queue("queue.order.die", true, false,false, null);
    }

    @Bean
    @Autowired
    public Binding bindingOrderDie(Queue queueOrderDie, Exchange exchangeOrderDie){
        return BindingBuilder.bind(queueOrderDie).to(exchangeOrderDie).with("key.order.die").noargs();
    }


    @Bean
    public Exchange exchangeOrder(){
        return new DirectExchange("ex.order", true, false);
    }

    @Bean
    public Queue queueOrder(){
        // ttl
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-message-ttl", 10_000);
        args.put("x-dead-letter-exchange", "ex.order.die");
        args.put("x-dead-letter-routing-key", "key.order.die");
        return new Queue("queue.order", true, false,false, args);
    }

    @Bean
    @Autowired
    public Binding bindingOrder(Queue queueOrder, Exchange exchangeOrder){
        return BindingBuilder.bind(queueOrder).to(exchangeOrder).with("key.order").noargs();
    }
}
